//
//  SJAFNetTool.m
//  Leaf Red Video Player
//
//  Created by 孙健 on 16/3/7.
//  Copyright © 2016年 孙健. All rights reserved.
//

#import "SJAFNetTool.h"
#import "AFNetworking.h"
@implementation SJAFNetTool

+ (void)GEtNetWithUrl:(NSString *)url WithBody:(id)body headerFile:(NSDictionary *)header response:(SJResponseStyle)responseSytle success:(BlockOfSuccess)success failure:(void (^)(NSError *))failure{
    
//    1.设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    2.设置请求头
    if (header) {
        
        for (NSString *key in header.allKeys) {
            
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
        
    }
//    3.设置返回数据的类型
    switch (responseSytle) {
        case SJJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case SJXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case SJDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            
            break;
            
        default:
            break;
    }
//     4.设置响应数据的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
//     5. utf - 8 转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    
    path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld.plist", [url hash]]];
    
//     6.请求数据
    [manager GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject) {
            success(responseObject);
            if (responseObject) {
            [NSKeyedArchiver archiveRootObject:responseObject toFile:path];

            }
                     }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error) {
          id result =  [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            if (result) {
                success(result);
            }
            
        }
    }];
    
    
}

+ (void)POSTNetWithUrl:(NSString *)url WithBody:(id)body bodyStyle:(SJRequestStyle)bodyRequestStyle headerFile:(NSDictionary *)header response:(SJResponseStyle)responseSytle success:(BlockOfSuccess)success failure:(void (^)(NSError *))failure{
    
    //    1.设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    2.设置body返回数据的类型
    switch (bodyRequestStyle) {
        case SJRequestJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case SJRequestString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                
                return parameters;
            }];
            break;
            
        default:
            break;
    }
    //    3.设置请求头
    if (header) {
        
        for (NSString *key in header.allKeys) {
            
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
        
    }
    switch (responseSytle) {
        case SJRequestJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case SJXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case SJDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            
            break;
            
        default:
            break;
    }

   
    //     4.设置响应数据的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
    //     5. utf - 8 转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    //     6.请求数据
    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error) {
            failure(error);
        }
    }];
    
    
}



@end
